﻿var arrFrames=[];
function pageInit(){
    try { picFocus_('fImgs', 'fTxts', 'fBtns', 'keyNews', '5000'); } catch (e) { }
    lazyLoad.init();
    setTab();
}
//Common
function setTab(){
    var objData=$$('disData'),
        tags=document.getElementsByName('navItem');
    var len=tags.length,
        j=0;
    for(var i=0;i<len;i++){
        var _obj=tags[i];
        var _objNext=getNextSibling(_obj);
        var typeid=_obj.id.split('_')[1],
            typename=_obj.title;
        _obj.onclick=function(){
            return setFrame(this);
        }
        _objNext.onclick=function(){
            return setFrame(this.previousSibling);
        }
        if(_obj.className=='chked') {
            getFrame(objData,typeid,typename);
            arrFrames[j]=typeid;
            j++;
        }
    }
}
function setFrame(obj){
    var typeid=obj.id.split('_')[1],
        typename=obj.title,
        css=obj.className;
    var frameID='frame_'+typeid;
    var objFrame=$$(frameID);
    if(objFrame){
        objFrame.parentNode.removeChild(objFrame);
        obj.className='chk';
        chkColumn(typeid,typename,0);
    }else{
        getFrame('disData',typeid,typename);
        chkColumn(typeid,typename,1);
        obj.className='chked';
        return true;
    }
}

function cancelFrame(typeid,typename)
{
    var tipinfo='确定要取消“'+typename+'”栏目？'
    if(!confirm(tipinfo)) return;
    
    var frameID='frame_'+typeid,
        kid='kid_'+typeid;
    var objFrame=$$(frameID),
        objKid=$$(kid);
    if(objFrame) objFrame.parentNode.removeChild(objFrame);
    if(objKid){
        if(objKid.className=='chked') objKid.className='chk';
    }
    chkColumn(typeid,typename,0);
}

function chkColumn(typeid,typename,chk)
{
    var data='typeid='+typeid+'&typename='+escape(typename)+'&chk='+chk;
    $.ajax({
        type:'post',
        data:data,
        url:'/API/column/chk.ashx',
        success:function(msg){
//            if(msg=='ok'){
//                alert('删除成功。');
//                location=location.href;
//            }else{
//                alert('删除失败！');
//            }
        }
    });
}
function inputKey(obj) {
    var val=obj.value;
    if(val==obj.title){
        obj.value='';
    }
    obj.style.color='#333';
    obj.onblur=function(){
        if(this.value==''||this.value==this.title){
            this.style.color='#999';
            this.value=this.title;
        }
    }
} 
function addHtml(){
    var typeid=1,typename='国内';
    var obj=document.getElementById('cnt_1');
    obj.innerHTML='<!--#include file="/f/t.html"-->';
    tabHover('page_1','cnt_1','a','ul',150);
}
function moveFrame(typeid, action) {
    var index = getIndex(typeid),
        swapIndex;
    if (index == 0 && action == 1) {
        alert('已在第一，不能再向前'); return;
    } else if (index == (arrFrames.length - 1) && action == 0) {
        alert('已在最后，不能再向后'); return;
    }
    var frameID,
        previousID;
    if (action == 1) {
        swapIndex = index - 1;
        frameID = typeid,
        swapID = arrFrames[swapIndex];
    } else {
        swapIndex = index + 1;
        frameID = arrFrames[swapIndex];
        swapID = typeid;
    }
    $('#frame_' + frameID).animate({
        top: '-=318px'
    }, 'fast');
    $('#frame_' + swapID).animate({
        top: '+=318px'
    }, 'fast', function () {
        var data = 't1=' + frameID + '&t2=' + swapID;
        $.ajax({
            type: 'post',
            data: data,
            url: '/API/url/swapOrder.ashx',
            success: function (msg) {
                //msg=='ok' is success
            },
            error: function () {
                alert('更改顺序失败，请检查网络链接');
            }
        });
    });
    arrFrames[index] = arrFrames[swapIndex];
    arrFrames[swapIndex] = typeid;
}
function getIndex(typeid){
    var len=arrFrames.length;
    for(var i=0;i<len;i++){
        if(typeid==arrFrames[i]) return i;
    }
}
function getFrame(objData,typeid, typename)
{
    if(typeof(objData)=='string') objData=$$('disData');
    var html = '',
        frameID='frame_'+typeid,
        cntID='cnt_'+typeid,
        pageID='page_'+typeid,
        keyID='keys_'+typeid;
    
    if($$(frameID)) return;
        
    var div=document.createElement('div');
    div.id=frameID;
    div.className='frame';    
    html='<div class="leftPanel l"><a name="'+typename+'" href="javascript:void(0)" coords="0"></a>\
          <div class="tit1" id="key_tit_'+typeid+'">\
          <span>\
          <a href="javascript:void(0)" title="向上移动" onclick="moveFrame('+typeid+',1)"><img src="/images/transparent.gif" class="icoUpArrow" /></a>\
          <a href="javascript:void(0)" title="向下移动" onclick="moveFrame('+typeid+',0)"><img src="/images/transparent.gif" class="icoDownArrow" /></a>\
          <a href="javascript:void(0)" title="取消定制" onclick="cancelFrame('+typeid+',\''+typename+'\')"><img src="/images/transparent.gif" class="icoDel" /></a>\
          </span>'+typename+'</div>\
          <div class="tit2"><span>\
          <a href="javascript:void(0)" onclick="getRecommendSites('+typeid+',1)">换一换</a>|\
          <a href="javascript:void(0)" onclick="openAddUrlWindow('+typeid+')">添加</a>\
          </span>点击加号关注网址</div>\
          <div id="urls_'+typeid+'" class="tags"></div>\
          <div class="dot1"></div>\
          <div id="keys_'+typeid+'" class="keysPanel">\
            <form id="addKeyForm" name="addKeyForm" action="#" onsubmit="return false">\
              <input id="addKey_'+typeid+'" type="text" class="inputTxt" style="width:120px;color:#999;" value="输入关注的关键词" title="输入关注的关键词" onkeydown="inputKey(this)" onkeyup="inputKey(this)" onkeypress="inputKey(this)" onfocus="if(this.value==this.title){setCursorPosition(this,0);}" onclick="if(this.value==this.title){setCursorPosition(this,0);}" />\
               <input id="submitAddKeyForm" type="submit" value="确定" class="btnmin" onclick="return chkAddKey('+typeid+',this)" /><span style="display:none;"></span>\
           </form>\
          </div><div class="dot1"></div>\
          <!--left end--></div>\
          <div class="midPanel r">\
          <div class="tab" id="tit_'+typeid+'"><a href="javascript:void(0)" class="over" coords="0"><strong>'+typename+'</strong></a></div><div id="cnt_'+typeid+'"></div>\
          </div><div class="space"></div>';
    div.innerHTML=html;    
    objData.appendChild(div);
    
    if(getCookie('Hvje'))
    {
        getNews(typeid,0);
    }else{
        getNewsHtml(typeid);
    }
    if(typeid==9)
    {
        var objKeys=$$(keyID);
        var elDiv=document.createElement('div'),
            elScript=document.createElement('script'),
            ajaxUrl='/js/select/type_'+typeid+'.js?v=1.4';
        elScript.type='text/javascript';
        elScript.id='jsCars';
        elDiv.innerHTML='<div class="dot1"></div><select id="carBrand" style="width:110px;"><option value="0" selected="true">-选择汽车品牌-</option></select>\
        <select id="carSerial" style="width: 80px"><option  value="0" selected="true">-选择车系-</option></select>';
        
        objKeys.appendChild(elDiv);
        $.ajax({
            type:'get',
            url:ajaxUrl,
            success:function(html){
                elScript.text=html;
                document.body.appendChild(elScript);
                loadSelectData(carBrand,'arrType_'+typeid);
            }
        });
        
    }
    getUserFollow(typeid,50);
    
    getRecommendSites(typeid);
    //getRecommendKeys(typeid);
}

function loadSelectData(id,data)
{
    var obj=(typeof(id)=='string'?$$(id):id);
    var objSub=getNextSibling(obj);
    if(!obj) return;
    obj.options.length=0;
    var arr=eval(data);
    var len=arr.length;
    obj.options.add(new Option('-选择汽车品牌-',0));
    for(var i=0;i<len;i++)
    {
        var _json=arr[i];
        obj.options.add(new Option(_json.name,_json.id));
    }
    obj.value=0;
    obj.onchange=function(){
        var val=this.value;
        for(var j=0;j<len;j++){
            var _data=arr[j];
            if(_data.id==val){
                loadSubSelect(objSub,_data.items);
                break;
            }
        }
    }
}
function loadSubSelect(obj,data)
{
    var len=data.length;
    if(len<1) return;
    obj.options.length=0;
    obj.options.add(new Option('-选择车系-',0));
    for(var i=0;i<len;i++)
    {
        obj.options.add(new Option(data[i].name,data[i].name));
    }
    obj.value=0;
    obj.onchange=function(){
        if(this.value!=0) addKey(null,9,this.value,selectKeyCallback)
    }
}
function getNewsHtml(typeid)
{
    $.ajax({
        type:'get',
        url:'/include/t_'+typeid+'.htm',
        success:function(html){
            
            var cntID='cnt_'+typeid,
                newsID='#news_'+typeid,
                itemsID='#items_'+typeid,
                nextID='#next_'+typeid,
                preID='#pre_'+typeid;
                
            $$(cntID).innerHTML=html;            
            $(newsID).scrollable({items:itemsID,
                            vertical:true,
                            keyboard:false,
                            mousewheel:true,
                            prev:preID,
                            next:nextID}).navigator({
				                navi: "#scrollbar_"+typeid,
				                naviItem: 'a',
				                activeClass: 'cur',
				                history: true
	                        });
	        setPageHeight(typeid);
        }
    });
}

function getUserFollow(typeid,num){
    if(!num) num=50;
    var data='typeid='+typeid+'&num='+num;
    $.ajax({
        type:'get',
        data:data,
        url:'/API/follow/get.ashx',
        success:function(data){
            parseUserFollow(typeid,data);
        }
    });
}

function parseUserFollow(typeid,data){
    var titID='tit_'+typeid,
        menuID='menu_'+typeid,
        sites='',
        menuItems='';
    var objTit=$$(titID),
        objMenu=$$(menuID);
        
    if(data.length>20){
        var json=eval('('+data+')');
        var len = json.length,
            titleWidth = 740,
            wordWidth=57;
        if(len>0){
            var arr=json;
            for(var i=0;i<len;i++){
                var dr=arr[i],url,name;
                if(dr['url']){
                    url=dr['url'],
                    name=dr['sitename'].decodeURI().Trim();
                }else{                
                    url=dr['key'].decodeURI().Trim(),
                    name=url;
                }
                wordWidth += (name.length * 12 + 21);
                if (wordWidth < 740) {
                    sites += '<a href="javascript:void(0)" coords="' + url + '" title="' + name + '">' + name + '<img src="/images/transparent.gif" class="del" alt="删除" onclick="delItem(this,' + typeid + ')" /></a>';                    
                }
                else {
                    menuItems += '<li><a href="javascript:void(0)" coords="' + url + '" title="' + name + '">' + name + '<img src="/images/transparent.gif" class="del" alt="删除" onclick="delItem(this,' + typeid + ')" /></a></li>';
                }
            }
        }
    }
    sites='<div id="more_'+typeid+'" class="menuUrl"><ul id="menu_'+typeid+'">'+menuItems+'</ul><ins onclick="OpenMenu(\'menu_'+typeid+'\');"><img src="/images/transparent.gif" class="arrow" alt="更多" /></ins></div>'+sites;    
    objTit.innerHTML+=sites;
    newsTab(typeid);
}
function newsTab(typeid,def)
{
    var titID='tit_'+typeid,
        cntID='cnt_'+typeid,
        moreID='more_'+typeid,
        menuID='menu_'+typeid;
    var objTit=$$(titID),
        objCnt=$$(cntID),
        objMore=$$(moreID),
        objMenu=$$(menuID);
    var objMoreNext=getNextSibling(objMore);

    var tits=objTit.getElementsByTagName('a'),
        menuItems=objMenu.getElementsByTagName('a');
    var len=tits.length,
        itemCount=menuItems.length,
        flag=0;
    for(var i=0;i<len;i++){
        tits[i].value=i;
        tits[i].className='';
        tits[i].onclick=function(){
            var _parentNode=this.parentNode,
                _param=this.getAttribute('coords'); 
            tits[flag].className='';
            flag=this.value;
            if(_parentNode.nodeName.toLowerCase()=='li'){
                var insertNode=this,
                    moveNode=getLastChild(objTit),
                    li=document.createElement('li');
     
               insertNode.innerHTML=insertNode.title.left(6,false)+'<img src="/images/transparent.gif" class="del" alt="删除" onclick="delItem(this,'+typeid+')" />';
               
                moveNode.innerHTML=moveNode.title+'<img src="/images/transparent.gif" class="del" alt="删除" onclick="delItem(this,'+typeid+')" />';
                
                //objTit.insertBefore(insertNode,objMoreNext);
                objTit.appendChild(insertNode);
                
                this.value=moveNode.value;
                flag=moveNode.value;
                
                moveNode.className='';
                moveNode.value=this.value;
                try{
                li.appendChild(moveNode);
                objMenu.insertBefore(li,getFirstChild(objMenu));
                _parentNode.parentNode.removeChild(_parentNode);
                }catch(e){alert(e);}
            }
            this.className='over';
            
            if(isURI(_param)||_param==0){
                getNews(typeid,_param);
            }else{
                getKeyNews(typeid,_param);
            }
        }
    }
    if(def) {
        if(len<14)
            tits[def].onclick();
        else
            menuItems[itemCount-1].onclick();
    }else{
        tits[flag].className='over';
    }
}

function updateUrlTab(obj,typeid,url,sitename){
    if(obj){
        $(obj).hide('slow');
        var objParent=obj.parentNode;
        objParent.parentNode.removeChild(objParent);
    }
    var objTit=$$('tit_'+typeid),
        objMenu=$$('menu_'+typeid),
        objMore=$$('more_'+typeid);
    var objMoreNex=getNextSibling(objMore);
    var tabs=objTit.getElementsByTagName('a');
    var len=tabs.length,
        menuItems=objMenu.getElementsByTagName('a').length;
    
    var a=document.createElement('a');
    a.href='javascript:void(0)';
    a.coords=url;
    a.title=sitename;
    a.value=len;
    a.innerHTML = sitename + '<img src="/images/transparent.gif" class="del" alt="删除" onclick="delItem(this,' + typeid + ')" />';

    var titleWidth = 740,
        currentTitleWidth = 0;
    $(objTit).children().each(function (i, o) {
        currentTitleWidth += o.width();
    });
    currentTitleWidth += 21 + sitename.length;
    try {
        console.log('currentTitleWidth=' + currentTitleWidth.toString());
    } catch (e) {console.log('e=' + e);}
    if (currentTitleWidth < titleWidth) {
        objTit.appendChild(a);
         newsTab(typeid,len);
    }else{
       var li=document.createElement('li');
       li.appendChild(a);
       objMenu.appendChild(li);
       newsTab(typeid,menuItems+1);
    }
}

function delItem(obj,typeid){
    if(!confirm('确定删除吗？')) return;
    var objTit=$$('tit_'+typeid);
    var objParent=obj.parentNode;
    var nodeName=objParent.parentNode.nodeName.toLowerCase(),
        param=objParent.getAttribute('coords'),
        len=objTit.getElementsByTagName('a');
        
    objParent.onclick=null;
    if(nodeName=='div'){
        objTit.removeChild(objParent);
    }else{
        var _objLi=objParent.parentNode;
        _objLi.parentNode.removeChild(_objLi);
    }
    if(isURI(param))
        delUrl(obj,typeid,param);
    else
        delKey(obj,typeid,param);
    newsTab(typeid);
}
//Common End

//Website      
function getRecommendSites(typeid,rnd){
    var id='urls_'+typeid,
        data = 'n=10&typeid=' + typeid;
    var obj=$$(id);
    if(rnd) data+='&rnd='+Math.round(Math.random()*1000);
    $.ajax({
        type:'get',
        data:data,
        url: '/API/site/recommend.ashx',
        success:function(data){
            obj.innerHTML=parseRecommendHtml(typeid,data);
        },
        error:function(){
            obj.innerHTML=loadedFailTxt;
        }
    })
}    
function parseRecommendHtml(typeid,data){
    if(data.length<15){
        return dataIsEmpty;
    }
    var empty=[],fail=[],
        json=eval('('+data+')'),
        html='';
    var siteNum=json.length;
    
    for(var i=0;i<siteNum;i++){
        var _site=json[i],css='chk';
        var _url=_site.url,
            _tit=_site.title.decodeURI(),
            _follow=_site.follow;
        
        html+='<label><a href="javascript:void(0)" onclick="';
        if(_follow==1){
            css='chked';
            html+='delFollowUrl';
        }else{
            html+='followUrl';
        }
        html+='(this,'+typeid+',\''+_url+'\',\''+_tit+'\')" class="'+css+'"></a><a href="'+_url+'" title="'+_tit+'" target=\"_blank\">'+_tit.left(10,false)+'</a></label>';
    }
    return html;
}

function getNews(typeid, url) {
    var id = 'cnt_' + typeid;
    var obj = $$(id);
    if (!obj) return;
    obj.innerHTML = loadingTxt;
    var data = 'num=60&typeid=' + typeid,
        action = '/API/Es/List.ashx';
    if (url != 0) {
        data += '&sites=' + url;
    }
    $.ajax({
        type: 'post',
        data: data,
        url: action,
        success: function (data) {
            if (data.length < 30) {
                obj.innerHTML = dataIsEmpty;
                return;
            }
            if (data == 'fail') {
                obj.innerHTML = loadFailTxt;
                return;
            }
            if (data == 'param') {
                obj.innerHTML = '引用参数失败，请<a href="javascript:void(0)" onclick="getNews(' + typeid + ',\'' + url + '\')">刷新</a>重试！';
                return;
            }
            var html,
                newsID = '#news_' + typeid,
                itemsID = '#items_' + typeid,
                nextID = '#next_' + typeid,
                preID = '#pre_' + typeid,
                pageID = "scrollbar_" + typeid;
            if (url != 0)
                html = parsePageHtml(typeid, data, false, 46);
            else
                html = parsePageHtml(typeid, data, true, 34);

            obj.innerHTML = html;
            $(newsID).scrollable({ items: itemsID,
                vertical: true,
                keyboard: false,
                mousewheel: true,
                prev: preID,
                next: nextID
            }).navigator({
                navi: '#' + pageID,
                naviItem: 'a',
                activeClass: 'cur',
                history: true
            });

            setPageHeight(typeid);

        },
        error: function () {
            obj.innerHTML = '数据读取失败，请点击<a href="javascript:void(0)" onclick="getNews(' + typeid + ',\'' + url + '\')">刷新</a>重试！';
        }
    });
}
function setPageHeight(typeid){
    var id="scrollbar_"+typeid;
    var obj=$$(id);
    if(obj){
        var tags=obj.getElementsByTagName('a'),
            h=278;
        var pageNum=tags.length;
        if(pageNum<6)
        {
            var aH=parseInt(h/pageNum);
            for(var i=0;i<pageNum;i++){
                tags[i].style.height=aH+'px';
            }
        }
    }
}
function handleTime(timeStr) {
    var arr = timeStr.toString().split('.'),
        datetimeStr = arr[0].replace('T', ' ');
    datetimeStr = datetimeStr.replace(/(\d{4})(\d{2})/, '$1-$2-').decodeURI();
    return datetimeStr;
}
function parsePageHtml(typeid,data,showSource,wordNum){  
    var empty=[],fail=[];
    var json=eval('('+data+')'),
        html='',
        pagination='<div class="scrollbar" id="scrollbar_'+typeid+'">',
        css='';
    var txtJson = json.txt,
        picJson = json.pic;
    var recordcount=txtJson.length,
        pageSize=10,
        picRecordcount=picJson.length,
        picPageSize=2;
    var num=60,
        pageNum=parseInt((recordcount+pageSize-1)/pageSize);
    var arr=getArrayRandomItems(txtJson,num);
    for(var p=1;p<=pageNum&&p<=6;p++){        
        var i=p*pageSize-pageSize,
            endNum=pageSize*p;
            
        html+='<dl><dt><ul>';
        for(;i<endNum&&i<recordcount;i++){
            var dr=arr[i];
            if(!dr) continue;
            var _arr = dr.url.split('/');
            html+='<li>·';
//            if(showSource==true){
            //  html+='<em>[<a href="'+dr.host+'" title="'+_source+'" target="_blank">'+_source.left(8,false)+'</a>]</em>';
//            }
            html += '<a href="/' + dr.url + '" title="' + dr.title.decodeURI() + '" target="_blank">' + (wordNum ? dr.title.decodeURI().left(wordNum, false) : dr.title.decodeURI()) + '</a><i>' + handleTime(dr.published_date) + '</i></li>';
        }
        html += '</ul></dt><dd><ul>';

        var j = p * picPageSize - picPageSize,
            picEndNum = picPageSize * p;
        for (; j < picEndNum && j < picRecordcount; j++)
        {
            var dr=picJson[j];
            if(!dr) continue;
            html += '<li><a href="/' + dr.url + '" title="' + dr.title.decodeURI() + '" target="_blank">\
                <img src="/img/170/120/http://219.232.240.226:9080/images/' + dr.thumb + '" /><br /><span>' + (wordNum ? dr.title.decodeURI().left(wordNum, false) : dr.title.decodeURI()) + '</a></span></li>';
        }
        html+="</ul></dd></dl>";
        if(p!=1)
            pagination+='<a href="javascript:void(0)"></a>';
        else
           pagination+='<a href="javascript:void(0)" class="cur"></a>'; 
    }
    pagination+='</div>';
    html='<div id="news_'+typeid+'" class="scrollable"><div class="news items" id="items_'+typeid+'">'+html+'</div></div>\
    <div class="fun">\
    <div class="page"><a id="pre_'+typeid+'" href="javascript:void(0)" class="prev"></a>\
    <a id="next_'+typeid+'" href="javascript:void(0)" class="next"></a></div>'+pagination+'</div>';
    return html;
}
function getPicNews(typeid){        
    var id='pic_'+typeid;
    var obj=$$(id);
    if(!obj) return;
    obj.innerHTML=loadingTxt;
    var data='num=3&typeid='+typeid;
    $.ajax({
        type:'post',
        data:data,
        url:'/API/url/news/pic.ashx',
        success:function(data){
            if(data=='empty'||data=='[]'){
                obj.innerHTML=dataIsEmpty;
            }else if(data=='fail'){
                obj.innerHTML=loadFailTxt;
            }else if(data=='param'){
                obj.innerHTML='引用参数失败，请<a href="javascript:void(0)" onclick="getNews('+typeid+',\''+url+'\')">刷新</a>页面重试！';
            }else{
                obj.innerHTML=parsePicHtml(data,false,24);
            }
        },
        error:function(){
             obj.innerHTML=loadedFailTxt;
        }
    });
}
function parsePicHtml(data,showSource,wordNum){
    var json=eval('('+data+')'),
        html='<dl><dt>';
    var len=json.length;
    for(var i=0;i<len&&i<3;i++){
        var dr=json[i];
        if(!dr) continue;
        var _url=dr[0],
            _title=dr[1].decodeURI().replace(/(·|\.)*$/,''),
            _time=dr[2],
            _imgpath=dr[3],
            _source=dr[4].decodeURI();
        var _arr=_url.split('/');
        var _sourceUrl=_arr[0]+'//'+_arr[2];
        if(showSource==true){
            html+='<em>[<a href="'+_sourceUrl+'" title="'+_source+'" target="_blank">'+_source.left(8,false)+'</a>]</em>';
        }
        html+='<a href="/'+_url+'" title="'+_title+'" target="_blank">\
        <img src="/img/'+_imgpath+'" onload="AutoResizeImage(88,66,this)" width="88" height="66" />\
        <span>'+(wordNum?_title.left(wordNum,false):_title)+'</span></a>';
    }
    html+='</dt></dl>';
    return html;
}
function delUrl(obj,typeid,url){
    $.ajax({
        type:'post',
        data:'typeid='+typeid+'&url='+url,
        url:'/API/url/del.ashx',
        success:function(msg){
            if(msg=='ok'){
                //删除成功
            }
        }
    });
}
function openAddUrlWindow(typeid){
    var openWindow=ZHAO.Dialog;
    openWindow.style='width:400px;height:200px;background:#FFF;border:1px #CCC solid';
    openWindow.innerHTML='<div class="popPanel">\
                <div class="popTit">\
                    <strong>添加关注网址</strong>\
                    <span><a href="javascript:void(0)" onclick="ZHAO.Dialog.Close()">\
                    <img src="/images/transparent.gif" class="icoClose" alt="关闭" /></a></span>\
                </div>\
                <div class="popBody">\
                    <form id="addUrlForm" name="addUrlForm" action="#" onsubmit="return false">\
                        <input id="typeid" value="'+typeid+'" type="hidden" />\
                        <p><label>网 址：</label><input id="newurl" type="text" class="inputTxt" size="20" /><span>请以http://</span></p><p><label>名 称：</label><input id="newsitename" type="text" class="inputTxt" /><span></span></p>\
                        <p><label></label><input id="submitAddUrlForm" type="submit" value="确定" class="btnGray" onclick="return chkAddUrl('+typeid+')" /> <input id="submitAddUrlForm" type="reset" value="取消" class="btnGray"  onclick="ZHAO.Dialog.Close()" /></p>\
                        <p id="tipAddUrl" style="display:none;"></p>\
                    </form>\
                </div>\
            </div>';
    openWindow.Open();
}
function chkAddUrl(typeid){
    var objName=$$('newsitename'),
        objUrl=$$('newurl'),
        objTip=$$('tipAddUrl'),
        objUrlTag=$$('urls_'+typeid);
        
    var objNameTip=getNextSibling(objName),
        objUrlTip=getNextSibling(objUrl);        
    if(objUrl.value.length<1){
        objUrlTip.innerHTML='<span class="err">网址不能空</span>';
        return false;
    }else if(!isURI(objUrl.value)){
        objUrlTip.innerHTML='<span class="err">网址不正确</span>';
        return false;
    }
    objUrlTip.innerHTML='';
    if(objName.value.length<1){
        objNameTip.innerHTML='<span class="err">名称不能为空</span>';
        return false;
    }
    objNameTip.innerHTML='';
    
    if(objUrl.value.indexOf('http://')!=0){
        objUrl.value='http://'+objUrl.value;
    }
    var url=objUrl.value,
        sitename=objName.value;
    var data='typeid='+typeid+'&sitename='+escape(sitename)+'&url='+url;
    $.ajax({
        type:'post',
        data:data,
        url:'/API/url/add.ashx',
        success:function(msg){
            if(msg=='ok'){
                updateUrlTab(null,typeid,url,sitename);
                ZHAO.Dialog.Close();
            }else{
                objTip.innerHTML='<span class="err">添加失败</span>';
                objTip.style.display='block';
            }
        }
    });
}
function followUrl(obj,typeid,url,sitename){  
    var data='typeid='+typeid+
             '&url='+escape(url)+
             '&sitename='+escape(sitename);
    $.ajax({
        type:'post',
        data:data,
        url:'/ajax/url/follow/add.ashx',
        success:function(msg){
            if(msg=='ok'){
               updateUrlTab(obj,typeid,url,sitename);
            }else{
               alert('添加失败');
            }
        },
        error:function(){
            alert('服务端请求失败');
        }
    });
}
function delFollowUrl(obj,typeid,url,sitename){
    var data = 'typeid=' + typeid +
             '&url=' + escape(url);
    $.ajax({
        type:'post',
        data:data,
        url:'/ajax/url/follow/del.ashx',
        success:function(msg){            
            obj.className='chk';
            obj.onclick=function(){
                followUrl(obj,typeid,url,sitename);
            }
            var objTit=$$('tit_'+typeid);
            var tabs=objTit.getElementsByTagName('a');
            var len=tabs.length;
            
            for(var i=0;i<len;i++){
                var _obj=tabs[i]
                var nodeName=_obj.parentNode.nodeName.toLowerCase(),
                    param=_obj.getAttribute('coords');
                if(param==url){
                    if(nodeName=='div'){
                        objTit.removeChild(_obj);
                    }else{
                        var _objLi=_obj.parentNode;
                        _objLi.parentNode.removeChild(_objLi);
                    }
                    newsTab(typeid);
                    break;
                }
            }
            if(msg=='ok'){
            }else{
                //alert('删除失败');
            }
        },
        error:function(){
            alert('服务端请求失败');
        }
    });
}

//Keywords
function getRecommendKeys(typeid){
    var id='keys_'+typeid;
    var obj=$$(id);
    $.ajax({
        type:'get',
        data:'typeid='+typeid,
        url:'/API/keyword/recommend.ashx',
        success:function(data){
             obj.innerHTML=parseRecommendKeysHtml(typeid,data);
        },
        error:function(){
             obj.innerHTML=loadedFailTxt;
        }
    })
}    
function parseRecommendKeysHtml(typeid,data){ 
    if(data==''||data.indexOf('param')!=-1) return dataIsEmpty;
    var empty=[],fail=[],
        json=eval('('+data+')'),
        html='';
    var keyNum=json.length;
    
    for(var i=0;i<keyNum;i++){
        var _data=json[i];
        var _key=_data.key,
            _isFollow=_data.isFollow;
        var css='chk';
        html+='<label><a href="javascript:void(0)" onclick=\"';
        
        if(_isFollow==1){
            html+='delFollowKey';
            css='chked';
        }else{
            html+='selectKey';
        }
        html+='(this,'+typeid+',\''+_key+'\')\" class="'+css+'"></a><a href="javascript:void(0)" title="'+_key+'">'+_key.left(12,false)+'</a></label>';
    }       
    return html;
}
function getKeyNews(typeid,key){
    var id='cnt_'+typeid,
        pageID='page_'+typeid;
    var obj=$$(id),
        objPage=$$(pageID);
    if(!obj) return;
    obj.innerHTML=loadingTxt;
    var data='num=60&typeid='+typeid,
        action='/API/Es/List.ashx';
    if (key != 0) {
        data += '&key=' + escape(key);
    }
    $.ajax({
        type:'get',
        data:data,
        url:action,
        success:function(data){
            if(data=='empty'||data=='[]'){
                obj.innerHTML=dataIsEmpty;
            }else if(data=='fail'){
                obj.innerHTML=loadFailTxt;
            }else if(data=='param'){
                obj.innerHTML='引用参数失败，请<a href="javascript:void(0)" onclick="top.location=top.location.href">刷新</a>页面重试！';
            }else{
                var html,
                    newsID='#news_'+typeid,
                    itemsID='#items_'+typeid,
                    nextID='#next_'+typeid,
                    preID='#pre_'+typeid;
                html=parsePageHtml(typeid,data,true,34);
                obj.innerHTML=html;
                $(newsID).scrollable({items:itemsID,
                            vertical:true,
                            keyboard:false,
                            mousewheel:true,
                            prev:preID,
                            next:nextID}).navigator({
				                navi: "#scrollbar_"+typeid,
				                naviItem: 'a',
				                activeClass: 'cur',
				                
				                history: true
	                        });
	           setPageHeight(typeid);
            }
        },
        error:function(){
             obj.innerHTML=loadedFailTxt;
        }
    });
}
function selectKey(obj,typeid,key){
    var titID='key_tit_'+typeid,
        cntID='key_cnt_'+typeid,
        moreID='key_more_'+typeid,
        menuID='key_menu_'+typeid;
    var objTit=$$(titID),
        objCnt=$$(cntID),
        objMore=$$(moreID),
        objMenu=$$(menuID);
        
    var data='typeid='+typeid+'&key='+key;
    addKey(obj,typeid,key,selectKeyCallback);
}

function selectKeyCallback(obj,typeid,key,msg){
   if(msg!=''&&msg.indexOf('Err')!=-1){
        alert('添加关键字失败：\r\n\r\n·您已经添加了“'+key+'”。');
        return;
   }
   
   var objParent,
       objTit=$$('tit_'+typeid),
       objMenu=$$('menu_'+typeid);
   var tabs=objTit.getElementsByTagName('a');
   var len=tabs.length;
   if(obj){
       objParent=obj.parentNode;
       objParent.parentNode.removeChild(objParent);
//       obj.className='chked';
//       obj.onclick=function(){
//           delFollowKey(this,typeid,key) 
//       }
   } 
   var a=document.createElement('a');
   a.href='javascript:void(0)';
   a.coords=key;
   a.title=key;
   a.value=len;
   a.innerHTML=key.left(6,false);
   if(len<8){
       a.innerHTML+='<img src="/images/transparent.gif" class="del" alt="删除" onclick="delItem(this,'+typeid+')" />';
       objTit.appendChild(a);
   }else{
      var li=document.createElement('li');
      li.appendChild(a);
      objMenu.appendChild(li);
   }
   newsTab(typeid,len);
}
//添加新的关键字
function openAddKeyWindow(typeid){
    
    var openWindow=ZHAO.Dialog;
    openWindow.style='width:280px;height:120px;background:#FFF;border:1px #CCC solid';
    openWindow.innerHTML='<div class="popPanel">\
                <div class="popTit">\
                    <strong>添加关键词</strong>\
                    <span><a href="javascript:void(0)" onclick="ZHAO.Dialog.Close()">\
                    <img src="/images/transparent.gif" class="icoClose" alt="关闭" /></a></span>\
                </div>\
                <div class="popBody">\
                    <form id="addKeyForm" name="addKeyForm" action="#" onsubmit="return false">\
                        <p><input id="typeid" value="'+typeid+'" type="hidden" /><input id="addKey_'+typeid+'" type="text" class="inputTxt" style="width:140px" />\
                        <input id="submitAddKeyForm" type="submit" value="确定" class="btnGray" onclick="return chkAddKey('+typeid+')" /> <input id="submitAddUrlForm" type="reset" value="取消" class="btnGray"  onclick="ZHAO.Dialog.Close()" /></p>\
                        <p id="tipAddKey" style="display:none;"></p>\
                    </form>\
                </div>\
            </div>';
    openWindow.Open();
}

function chkAddKey(typeid,obj){
    try{
        var objAddKey=$$('addKey_'+typeid),
            objTip=$$('tipAddKey'),
            tipinfo=null;
        if(obj) objTip=getNextSibling(obj);
        var newKey=objAddKey.value;
        if(newKey.length<2||newKey==objAddKey.title){
            tipinfo='请输入关键字';
        }else if(!isCnNumberLetter(newKey)){
            tipinfo='只能为字母、汉字';            
        }
        objAddKey.onblur=function(){
            if(objTip.innerHTML.length>2) objTip.innerHTML='';
        }
        if(tipinfo!=null){
            objTip.innerHTML='<span class="err">'+tipinfo+'</span>';
            objTip.style.display='inline-block';
            objAddKey.focus();
            return false;
        }else{
            objTip.style.display='none';
        }
        addKey(null,typeid,newKey,addKeyCallback);
        objAddKey.value='';
    }catch(e){alert(e);}
    return false;
}
function addKey(obj,typeid,key,callback){
    key=key.replace(/=+/g,'');
    
    var data='typeid='+typeid+'&key='+key;
    $.ajax({
        type:'post',
        data:data,
        url:'/API/keyword/add.ashx',
        success:function(msg){
            if(callback) callback(obj,typeid,key,msg);
        }
    });
}

function addKeyCallback(obj,typeid,key,msg){
   //添加关键字后调用
   var objAddKey=$$('addKey_'+typeid),
       objKeyTag=$$('keys_'+typeid);
       objTip=$$('tipAddKey');
   if(msg.indexOf('Err')!=-1){
       if(objTip) 
            objTip.innerHTML='<span class="err">添加失败：关键字可能已存在。</span>';
       else
            alert('添加失败：关键字可能已存在');
       return;
   }else if(objTip) objTip.innerHTML='';
   
   var objTit=$$('tit_'+typeid),
       objMenu=$$('menu_'+typeid);
   var tabs=objTit.getElementsByTagName('a');
   var len=tabs.length;

   var a=document.createElement('a');
   a.href='javascript:void(0)';
   a.coords=key;
   a.title=key;
   a.value=len;
   a.innerHTML=key.left(6,false)+'<img src="/images/transparent.gif" class="del" alt="删除" onclick="delItem(this,'+typeid+')" />';

   var titleWidth = 740,
        currentTitleWidth = 0;
   $(objTit).children().each(function (i, o) {
       currentTitleWidth += o.width();
   });
   currentTitleWidth += 21 + sitename.length;
   try {
       console.log('currentTitleWidth=' + currentTitleWidth.toString());
   } catch (e) { console.log('e=' + e); }
   if (currentTitleWidth < titleWidth) {        
       objTit.insertBefore(a,objTit.lastChild);
   }else{
      var li=document.createElement('li');
      li.appendChild(a);
      objMenu.appendChild(li);
   }
   //var el=document.createElement('label');
   //el.innerHTML='<a href="javascript:void(0)" onclick="delFollowKey(this,\''+typeid+'\',\''+key+'\')" class="chked"></a><a href="#'+key+'">'+key.left(12,false)+'</a>';
   //objKeyTag.insertBefore(el,objKeyTag.firstChild);
   
   newsTab(typeid,len);
   //ZHAO.Dialog.Close();
}

function delFollowKey(obj,typeid,key){
    var data='typeid='+typeid+'&key='+escape(key);
    $.ajax({
        type:'post',
        data:data,
        url:'/API/keyword/del.ashx',
        success:function(msg){
            if(msg=='ok'){
                obj.className='chk';
                obj.onclick=function(){
                    selectKey(this,typeid,key)
                }
                var objTit=$$('tit_'+typeid);
                var tabs=objTit.getElementsByTagName('a');
                var len=tabs.length;
                
                for(var i=0;i<len;i++){
                    var _obj=tabs[i]
                    var nodeName=_obj.parentNode.nodeName.toLowerCase(),
                        param=_obj.getAttribute('coords');
                    if(param==key){
                        if(nodeName=='div'){
                            objTit.removeChild(_obj);
                        }else{
                            var _objLi=_obj.parentNode;
                            _objLi.parentNode.removeChild(_objLi);
                        }
                        newsTab(typeid);
                        break;
                    }
                }
            }else{
                alert('删除失败');
            }
        },
        error:function(){
            alert('服务端请求失败');
        }
    });
}

function delKey(obj,typeid,key){
    var data='typeid='+typeid+'&key='+escape(key);
    $.ajax({
        type:'post',
        data:data,
        url:'/API/keyword/del.ashx',
        success:function(msg){
           if(msg=='ok'){
                //删除成功
           }
        }
    });
}

//栏目
function openAddColumnWindow()
{
   var openWindow=ZHAO.Dialog;
    openWindow.style='width:430px;height:200px;background:#FFF;border:1px #CCC solid';
    openWindow.innerHTML='<div class="popPanel">\
                <div class="popTit">\
                    <strong>添加新栏目</strong>\
                    <span><a href="javascript:void(0)" onclick="ZHAO.Dialog.Close()">\
                    <img src="/images/transparent.gif" class="icoClose" alt="关闭" /></a></span>\
                </div>\
                <div class="popBody">\
                    <form id="addColumnForm" name="addColumnForm" action="#" onsubmit="return false">\
                        <p><label>栏 目：</label><input id="typename" type="text" class="inputTxt" size="20" maxlength="10" /><span></span></p><p><label>类 型：</label><input id="news" name="property" type="radio" value="1" checked="checked" />统合资讯 <input id="industry" name="property" type="radio" value="2" />行业职业 <input id="interest" name="property" type="radio" value="3" />兴趣爱好</p>\
                        <p><label></label><input id="submitAddColumnForm" type="submit" value="确 定" class="btnGray" onclick="return chkAddColumn()" /> <input id="closeAddColumnWindow" type="button" value="取 消" class="btnGray" onclick="ZHAO.Dialog.Close()" /></p>\
                    </form>\
                    <div id="tipAddColumn"></div>\
                </div>\
            </div>';           
    openWindow.Open(); 
}
function chkAddColumn(){
    var objTip=$$('tipAddColumn'),
        objForm=$$('addColumnForm'),
        objName=$$('typename');
    var typename=objName.value,
        property=getRadioGroupVal('property');
    if(typename.Trim()==''){
        objTip.innerHTML='<span class="err">请输入栏目名称</span>';
        return false;
    }else{
        objForm.style.display='none';
        objTip.innerHTML='<p style="text-align:center;color:#999; vertical-align:middel;"><img src="'+loadingImg.src+'" /> 数据提交中...</p>';
    }
    
    addColumn(typename,property,null);
}
function addColumn(typename,property,callback)
{
    var data='typename='+escape(typename)+'&property='+property,
        objTip=$$('tipAddColumn');
    $.ajax({
        type:'post',
        data:data,
        url:'/API/column/add.ashx',
        success:function(msg){
            if(!isNaN(msg))
            {
                objTip.innerHTML='<span class="ok"> 栏目<b>'+typename+'</b>添加成功。<br />立即<a href="javascript:void(0)" onclick="openAddUrlWindow('+msg+')">添加网址</a>。</span>';
                getFrame('disData',msg, typename);                
            }
            else
            {
                objTip.innerHTML='<span class="err"> 栏目添加失败，您可能已定制了此栏目</span>';
            }
       }
    });
}
//栏目 结束

//提示登录
function openTipWindow()
{
   if(getCookie('vje')) return true;
   var openWindow=ZHAO.Dialog;
    openWindow.style='width:430px;height:130px;background:#FFF;border:1px #CCC solid';
    openWindow.innerHTML='<div class="popPanel">\
                <div class="popTit">\
                    <strong>请先登录</strong>\
                    <span><a href="javascript:void(0)" onclick="ZHAO.Dialog.Close()">\
                    <img src="/images/transparent.gif" class="icoClose" alt="关闭" /></a></span>\
                </div>\
                <div class="popBody">\
                    <div class="alert">　登录后才可以操作，如果你已有帐号请点击<a href="/user/login.aspx" target="spFrame" onclick="ZHAO.Dialog.Close()">登录</a>，如果没有帐号请点击<a href="/reg.aspx">立即注册</a>。</div>\
                </div>\
            </div>';           
    openWindow.Open();
    setTimeout('ZHAO.Dialog.Close()',15000);
    return false; 
}

//*menu start
var menuID='';
function OpenMenu(targ,time){    
    if(menuID!=targ) CloseMenu();
    menuID=targ;
    var obj=$$(menuID);  
	if(obj.style.display == 'block'){
		CloseMenu();
	}else{	    
		obj.style.display = 'block';
		setTimeout("addEvent('click',document.body,CloseMenu)",500);
		if(time) setTimeout(function t(){obj.style.display = 'none';},time);
	}
};

function CloseMenu(){
    var obj=$$(menuID);
    if(!obj) return;
	obj.style.display = 'none';
	delEvent(document.body,'click',CloseMenu);
};

function addEvent(eventType,obj,func){  
    if(obj.attachEvent){
        obj.attachEvent('on' + eventType,func);
    }else{
        obj.addEventListener(eventType,func,false)}
}

function delEvent(obj,eventType,func){
	if(obj.detachEvent){
	    obj.detachEvent('on' + eventType,func);
	}
	else{
	    obj.removeEventListener(eventType,func,false);
	}
}
//menu end